﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fif.arg
{
    public class Args
    {
        /// <summary>
        /// Argument prefix
        /// </summary>
        private const string ArgPrefix = "--";

        /// <summary>
        /// Argument key-value pair delimeter
        /// </summary>
        private const string ArgColon = "=";

        /// <summary>
        /// Argument value wrapper (to wrap text with spaces, or path with spaces)
        /// </summary>
        private const string ArgQuote = "'";

        /// <summary>
        /// What to search
        /// </summary>
        private const string ArgText = "text";

        /// <summary>
        /// Where to search
        /// </summary>
        private const string ArgPath = "path";

        /// <summary>
        /// Output file name
        /// </summary>
        private const string ArgFile = "file";

        /// <summary>
        /// Language for non-unicode files
        /// </summary>
        private const string ArgLang = "lang";

        /// <summary>
        /// Path to add to windows indexed paths
        /// </summary>
        private const string ArgIndex = "idx";

        public string Text { get; set; }

        public string Path { get; set; }

        public string File { get; set; }

        public string Lang { get; set; }

        public string Index { get; set; }

        public Args(string[] args)
        {
            Parse(args);
        }

        public void Validate()
        {
            try
            {
                Validate(Index, ArgIndex);
                return ;
            }
            catch(ArgumentException)
            {

            }

            Validate(Text, ArgText);
            Validate(Path, ArgPath);
            Validate(File, ArgFile);
            Validate(Lang, ArgLang);
        }

        private void Parse(string[] args)
        { 
            if (null == args || 0 == args.Length)
            {
                throw new ArgumentException("No arguments provided");
            }

            Text = Extract(ArgText, args);
            Path = Extract(ArgPath, args);
            File = Extract(ArgFile, args);
            Lang = Extract(ArgLang, args);
            Index = Extract(ArgIndex, args);
        }

        private static string Extract(string name, string[] args)
        {
            string value = null;
            var token = string.Format("{0}{1}{2}", ArgPrefix, name, ArgColon);
            var data = args.Where(x => x.StartsWith(token)).FirstOrDefault();
            if (!string.IsNullOrEmpty(data))
            {
                return data.Replace(token, string.Empty).Trim(ArgQuote.ToCharArray());
            }

            return value;
        }

        private static void Validate(string value, string name)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("Can not be null or empty", name);
            }
        }
    }
}
